package com.splic.api.controller.product;

import com.splic.common.core.DeviceContext;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.ShiroUtils;
import com.splic.common.utils.StringUtils;
import com.splic.product.dto.DeviceDto;
import com.splic.product.dto.DeviceReportResultDto;
import com.splic.product.facade.DeviceFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户设备信息
 */
@RestController
@RequestMapping("/api/device")
@Slf4j
public class ApiDeviceController extends BaseController {

    @Autowired
    private DeviceFacade deviceFacade;

    /**
     * 活跃或者新增数据上报
     * @param
     * @return
     */
    @PostMapping("/reportInfo")
    public ResponseObj<DeviceReportResultDto> reportInfo(@RequestBody DeviceDto device){
        try {
            if (StringUtils.isBlank(device.getAndroidId())
                    && StringUtils.isBlank(device.getMac())
                    && StringUtils.isBlank(device.getOaid())
                    && StringUtils.isBlank(device.getVaid())
                    && StringUtils.isBlank(device.getGaid())
                    && StringUtils.isBlank(device.getImei())
                    && StringUtils.isBlank(device.getDeviceIdentify())) {
                log.error("关键参数没有携带参数值，设备新增中。。。:产品{}设备{}", DeviceContext.getProductPackageKey(), device);
            }
            device.setUserId(ShiroUtils.getUserId());
            DeviceReportResultDto deviceReportResultDto= deviceFacade.reportInfo(device);
            return ResponseObj.SUCCESS(deviceReportResultDto);
        }catch (BusinessException e){
            log.error("上报失败:{}",e.getMessage());
            return ResponseObj.ERROR(e.getMessage());
        }catch (Exception e){
            log.error("上报失败:{}",e.getMessage());
            return ResponseObj.ERROR("设备注册连接失败。");
        }
    }
}
